<template>
	<view class="content">
		<scroll-view style="height: calc(100vh);" class="wid100" scroll-y="true" @scrolltolower="lower">
			<view class="hei20"></view>
			<view v-if="list.length == 0" class="wid90 mar fowe800 foSi35">
				<view style="height: 50rpx;"></view>
				<view class="dis disJuC">
					<image src="../../static/zanwu1.png" style="width: 400rpx;height: 300rpx;"></image>
				</view>
				<view class="textCen mar-top20" style="color: #868686;">暂无数据</view>
			</view>
			<block v-for="(item,index) in list" :key="index">
				<view v-if="item.status != 2" class="mar mar-bott20 dis coFFF poRel wid85" :style="'background-image: url('+url+'/addon/qianyu_petrol/wxapp/image/yhq_red.png);'"
					style="height: 190rpx;box-sizing: border-box;background-size: 100% 100%;">
					<view class="dis disAl disJuC" style="width: 180rpx;">
						<view class="foSi30 fowe800" style="vertical-align: bottom;color: #fff;">
							<span>¥</span>
							<span class="fowe600 foSi40">{{item.coupon.money}}</span>
						</view>
					</view>
					<view class="dis disAl mar-left30">
						<view class="textCen" style="width: auto;">
							<view class="foSi25">{{item.coupon.title?item.coupon.title:'活动消失了'}}</view>
							<view v-if="" class="foSi20 mar-top10 ">领取{{item.coupon.valid_days}}天内有效</view>
							<view class="foSi20 mar-top10">
								{{dateFormat(item.coupon.valid_days,item.createtime)}}
							</view>
						</view> 
					</view>
					<view class="poAbs hei60 borRad40 foSi30 fowe600 dis disAl disJuC butt1 butt2" @click="tiaozhuan('/pages/toiletries/toiletries')"
						v-if="item.status == 0">去使用</view>
					<view class="poAbs hei60 borRad40 foSi30 fowe600 dis disAl disJuC butt1 butt2"
						v-if="item.status == 1">已使用</view>
				</view>
				<view v-if="item.status == 2" @longpress="longpress(item.id)" class="mar mar-bott20 dis coFFF poRel wid85" :style="'background-image: url('+url+'/addon/qianyu_petrol/wxapp/image/yhq_black.png'"
					style="height: 190rpx;box-sizing: border-box;background-size: 100% 100%;">
					<view class="dis disAl disJuC" style="width: 180rpx;">
						<view class="foSi30 fowe800" style="vertical-align: bottom;color: #fff;">
							<span>¥</span>
							<span class="fowe600 foSi40">{{item.coupon.money}}</span>
						</view>
					</view>
					<view class="dis disAl mar-left30">
						<view class="foSi25">{{item.coupon.title}}</view>
					</view>
					<view class="poAbs hei60 borRad40 foSi30 fowe600 dis disAl disJuC butt1" style="background-color: #7D7D7D;">已过期</view>
				</view>
			</block>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				url: this.global.url,
				list: [],
				last_page: 0,
				page: 1,
			}
		},
		onLoad() {
			this.getList()
		},
		methods: {
			dateFormat(days,time) {
				let gqtime = time+days*86400
				let date = new Date(gqtime*1000);
				let year = date.getFullYear();
				// 在日期格式中，月份是从0开始的，因此要加0，使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
				let month = date.getMonth() + 1;
				// let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
				let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
				let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
				let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
				let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
				// 拼接
				// return month+". "+day;
				return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
			},
			getList() {
				var that = this;
				uni.request({
					url: that.global.url + '/api/qianyu_petrol/gasstation_coupon_log',
					method: "POST",
					header: {
						'site-id': that.global.site_id,//自定义请求头信息
						'token': uni.getStorageSync('token')
					},
					data: {
						page: that.page,
						limit: 10
					},
					success(res) {
						let result = res.data
						console.log(result)
						if (result.code == 1) {
							that.last_page = result.data.last_page
							that.list = that.list.concat(result.data)
						}
					}
				})
			},
			lower(e) {
				if(this.last_page > this.page){
					this.page++
					this.getList()
				}
			},
			tiaozhuan(url){
				uni.switchTab({
					url: url
				})
			},
			longpress(id){
				var that = this;
				uni.showModal({
	　　　　　　　　title: '删除',
	　　　　　　　　content: '是否要删除？',
	　　　　　　　　success(res) {
					if (res.confirm) {
						console.log('用户点击确定');
						console.log(id);
						
						uni.request({
							url: that.global.url + '/api/qianyu_petrol/gasstation_coupon_del',
							method: "POST",
							header: {
								'site-id': that.global.site_id,//自定义请求头信息
								'token': uni.getStorageSync('token')
							},
							data: {
								id:id
							},
							success(res) {
								let result = res.data
								if (result.code == 1) {
									
									that.list = []
									that.page = 1
									that.getList()
								}
							}
						})
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
	　　　　　　　　}
				})
			}
		}
	}
</script>

<style>
	.content {
		background-color: #F8F9FD;
		height: 100vh;
	}
	.butt1{
		right: 20rpx;
		top: 65rpx;
		width: 184rpx;
	}
	.butt2{
		background-color: #FFB969;
		color: #FC1E1B;
	}
</style>
